//
//  THJNewsDetailVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/11/8.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJNewsDetailVC.h"
#import "ZBNetworking.h"
#import <WebKit/WebKit.h>
#import <SDAutoLayout.h>
@interface THJNewsDetailVC ()<WKNavigationDelegate>
{
    UITableView * _tableview;
    WKWebView * _webview;
}
@property (weak, nonatomic)  CALayer *progresslayer;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (weak, nonatomic) IBOutlet UIButton *fabuBtn;

@end

@implementation THJNewsDetailVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController.navigationBar setHidden:YES];
    [self setStateBarColor:[UIColor whiteColor]];
    if(self.flag){
        self.fabuBtn.hidden = YES;
        self.textF.hidden = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if(KIsiPhoneX){
        self.topSpace.constant = 20;
    }
    [self setProgress];
//    [self setStateBarColor:[UIColor whiteColor]];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [MBProgressHUD showMessage:@"" toView:self.view];
    _webview = [[WKWebView alloc] init];
    _webview.navigationDelegate = self;
    
    _webview.frame = CGRectMake(0, 23, ScreenWidth, ScreenHeight-46-23);
    if (KIsiPhoneX) {
         _webview.frame = CGRectMake(0, 47, ScreenWidth, ScreenHeight-90-23);
    }
    [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
    [self.view addSubview:_webview];
   
    [_webview.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionOld context:nil];
    [_webview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
    // Do any additional setup after loading the view from its nib.
}
-(void)setProgress{
    CGFloat space;
    if(KIsiPhoneX){
        space = 44;
    }else{
        space = 20;
    }
    UIView *progress = [[UIView alloc]initWithFrame:CGRectMake(0, space, CGRectGetWidth(self.view.frame), 3)];
    progress.backgroundColor = [UIColor clearColor];
    [self.view addSubview:progress];
    
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(0, 0, 0, 3);
    layer.backgroundColor = COLOR(255, 97, 61, 1).CGColor;
    [progress.layer addSublayer:layer];
    self.progresslayer = layer;
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        self.progresslayer.opacity = 1;
        //不要让进度条倒着走...有时候goback会出现这种情况
        if ([change[@"new"] floatValue] < [change[@"old"] floatValue]) {
            return;
        }
        self.progresslayer.frame = CGRectMake(0, 0, self.view.bounds.size.width * [change[@"new"] floatValue], 3);
        if ([change[@"new"] floatValue] == 1) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.progresslayer.opacity = 0;
            });
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.progresslayer.frame = CGRectMake(0, 0, 0, 3);
            });
        }
    }else if ([keyPath isEqualToString:@"contentSize"]){
        //    NSString *location =[_webview stringByEvaluatingJavaScriptFromString:@"document.URL"];
        //    NSLog(@"OOO--%@",location);
        //设置JS
        NSString *inputValueJS = @"document.URL";
        //执行JS
        [_webview evaluateJavaScript:inputValueJS completionHandler:^(id _Nullable response, NSError * _Nullable error) {
            NSLog(@"value: %@ ", response);
        }];
    }

}
- (IBAction)fabuBtnAction:(id)sender {
    NSLog(@"点击发布");
    if(![THJTools IsLogIn]){
         NSLog(@"点击发布666");
        [self HUDShowMsg:@"请先登录" AfterDelayDimiss:1];
        return;
    }
    if(self.textF.text.length){
        NSLog(@"jjjjjjj");
        NSDictionary * dic = @{
                               @"mid":self.mid,
                               @"item_id":self.itemid,
                               @"content":self.textF.text
                               };
        NSString * url = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=comment&r=comment&m=add"];
        NSLog(@"p-%@",dic);
        [THJNetWork postDataFromUrl:url parameters:dic Token:[THJTools getToken] callback:^(NSDictionary* receiveData, NSError *error) {
            NSLog(@"pl-%@-%@",receiveData,[THJTools getToken]);
            if([receiveData[@"status"] isEqual:@"200"]){
                [self HUDShowMsg:@"发布成功" AfterDelayDimiss:1];
                _textF.text = @"";
                [_webview reload];
            }else{
                [self HUDShowMsg:@"发布成功" AfterDelayDimiss:2];
                _textF.text = @"";
                [_webview reload];
//                 [self HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
            }
            
        }];
    }else{
        [self HUDShowMsg:@"请输入评论内容" AfterDelayDimiss:1];
    }
}
- (IBAction)shareNewsAction:(id)sender {
    [self shareWithContent:self.newsContent Title:self.newsTitle Url:self.url];
}
- (IBAction)goBackBtnAction:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)dealloc{
    [_webview.scrollView removeObserver:self forKeyPath:@"contentSize"];
    [_webview removeObserver:self forKeyPath:@"estimatedProgress"];
}
@end
